package com.wthink.newMain.msg.inner

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.wthink.hzgj.R
import com.wthink.hzgj.data.domain.NoticeMsg
import com.wthink.hzgj.databinding.FragmentMsgInnerReaderBinding
import com.wthink.hzgj.databinding.FragmentMsgInnerUnreaderBinding
import com.wthink.hzgj.databinding.FragmentSchedlePlanBinding
import com.wthink.hzgj.util.ClickUtil
import com.wthink.hzgj.util.PreferencesUtils
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.hzgj.util.closeDefaultAnimator
import com.wthink.newMain.msg.inner.adapter.MsgAdapter
import com.wthink.newMain.msg.inner.viewmodel.ReadedViewmodel
import com.wthink.newMain.msg.inner.viewmodel.UnReadedViewmodel
import com.wthink.newMain.schedlePlan.viewmodel.SchedlePlanViewmodel
import com.wthink.newMain.view.MsgDialog
import java.text.FieldPosition

class UnReadFragment : Fragment(){

    private val msgDialog: MsgDialog by lazy {
        MsgDialog(context!!)
    }

    private val viewModel by viewModels<UnReadedViewmodel>()

    private lateinit var binding: FragmentMsgInnerUnreaderBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_msg_inner_unreader,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.start()

        binding.unreadedRecycle.layoutManager = LinearLayoutManager(context,
            LinearLayoutManager.VERTICAL,false)

        binding.unreadedRecycle.adapter = MsgAdapter().apply {
            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    val notFastClick = ClickUtil.isNotFastClick()
                    val noticeMsg = obj as NoticeMsg
                    if (notFastClick) {
                        saveList(noticeMsg)
                        showMsgDialog(obj as NoticeMsg)
                        //  viewModel.start()
                    }
                }
            }
        }
        closeDefaultAnimator(binding.unreadedRecycle)
    }

    private fun saveList(msg: NoticeMsg) {
        var stringToArrryLists : MutableList<NoticeMsg> = mutableListOf()
        val stringToList = PreferencesUtils.stringToList()
        stringToList.forEachIndexed { index, noticeMsg ->
            if (msg == noticeMsg) {
                stringToArrryLists.add(NoticeMsg(noticeMsg.msg,noticeMsg.time,noticeMsg.index,false,true))
            } else {
                stringToArrryLists.add(noticeMsg)
            }
        }
        PreferencesUtils.saveListToString(stringToArrryLists)
    }

    private fun showMsgDialog(noticeMsg: NoticeMsg) {
        msgDialog.show()
        msgDialog.setOnclickListener(View.OnClickListener { v ->
            when (v?.id) {
                R.id.close -> {
                    msgDialog.dismiss()
                    viewModel.start()
                   // binding.unreadedRecycle.adapter?.notifyDataSetChanged()
                }
            }
        })
        msgDialog.setMsgText(noticeMsg.msg!!)
    }
}